package com.renwei.yunlong.chartfragment;

import android.graphics.Color;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;

import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseFragment;

import butterknife.Bind;
import butterknife.ButterKnife;


public class PieChartFragment extends BaseFragment{

    @Bind(R.id.pieChart)
    PieChart mChart;
    @Override
    public View initView() {
        View view = LayoutInflater.from(mActivity).inflate(R.layout.fragment_pie_chart, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void initData() {
        mChart.getDescription().setEnabled(false);
        mChart.setRotationEnabled(false);
//        Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "OpenSans-Light.ttf");

        mChart.setCenterText(generateCenterText());
        mChart.setCenterTextSize(15f);
        mChart.setExtraOffsets(20.f, 0.f, 20.f, 0.f);
//        mChart.setCenterTextTypeface(tf);

        // radius of the center hole in percent of maximum radius
        mChart.setHoleRadius(55f);
        mChart.setTransparentCircleRadius(60f);

        Legend l = mChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        l.setDrawInside(false);
        l.setEnabled(true);

        mChart.setData(ChartData.generatePieData());
    }
    private SpannableString generateCenterText() {
        SpannableString s = new SpannableString("1467\n工单总数");
        s.setSpan(new RelativeSizeSpan(2f), 0, 4, 0);
        s.setSpan(new ForegroundColorSpan(Color.GRAY), 4, s.length(), 0);
        return s;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
